<template>
    <div>
        {{data}}
        <table class="table table-border">
            <tbody>
                <tr>
                    <td>饭店名称</td>
                    <td><input type="text" v-model="data.dname"></td>
                </tr>
                     <tr>
                    <td>分店名称</td>
                    <td><input type="text" v-model="data.fen"></td>
                </tr>
                     <tr>
                    <td>饭店地址</td>
                    <td><select v-model="data.cid">
                        <option value="">请选择</option>
                        <option :value="o.cid" v-for="o in lie" :key="o.cid">{{o.cname}}</option>
                    </select></td>
                </tr>
                      <tr>
                    <td>客服电话</td>
                    <td><input type="text" v-model="data.phone"></td>
                </tr>
                     <tr>
                    <td>饭店状态</td>
                    <td><input type="radio" :value="1" v-model="data.flag">启用
                    <input type="radio" :value="0" v-model="data.flag">停用</td>
                </tr>
                     <tr>
                    <td>饭店图片</td>
                    <td><input type="file" @change="img">
                    <img :src="data.tu" width="100px" height="100px" v-show="imgshow"></td>
                </tr>
                     <tr>
                    <td>饭店介绍</td>
                    <td><textarea v-model="data.jie"></textarea></td>
                </tr>
                     <tr>
                    <td>入店须知</td>
                    <td><textarea v-model="data.xu"></textarea></td>
                </tr>
                     <tr>
                    <td>交通信息</td>
                    <td><textarea v-model="data.jiao"></textarea></td>
                </tr>
                     <tr>
                    <td></td>
                    <td><input type="button" value="提交" @click="add()"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {onMounted, ref} from 'vue'
import moment from 'moment';
import axios from 'axios';
import {useRoute,useRouter} from 'vue-router'
const route = useRoute()
const router = useRouter()
let imgshow:any =ref(false)
let data:any = ref({
  "dname": "",
  "fen": "",
  "cid": '',
  "tu": "",
  "phone": "",
  "flag": 1,
  "jie": "",
  "xu": "",
  "jiao": ""
})
let lie:any = ref({

})
onMounted(()=>{
show()
})
const show = ()=>{
    axios({
        url:'https://localhost:7046/api/Dan/Chengs',
        method:'get'
    })
    .then(res=>{
        lie.value = res.data
    })
}
const add=()=>{
    if(data.value.dname=="")
    {
        alert('饭店名称不能为空')
                return;
    }
    if(data.value.cid=="")
    {
        alert('饭店地址不能为空')
                return;
    }
    if(data.value.phone=="")
    {
        alert('客服电话不能为空')
                return;
    }
    if(data.value.tu=="")
    {
        alert('饭店图片不能为空')
                return;
    }
    if(data.value.jie=="")
    {
        alert('饭店介绍不能为空')
                return;
    }
    if(data.value.xu=="")
    {
        alert('入店须知不能为空')
                return;
    }
    if(data.value.dname=="")
    {
        alert('饭店名称不能为空')
                return;
    }
    axios({
        url:'https://localhost:7046/api/Dan/Add',
        method:'post',
        data:data.value
    })
    .then(res=>{
        if(res.data>0)
        {
            alert('新增成功')
        }
        else
        {
                alert('新增失败')
                return;
        }
    })
}
const img=(e:any)=>{
    let p = e.target.files[0]
    let s = new FormData()
    s.append("file",p);
    axios({
        url:'https://localhost:7046/api/Dan/Tu',
        method:'post',
        data:s

    })
    .then(res=>{
        if(res.data=="图片格式不对")
        {
            alert("图片格式不对")
            return;
        }
        if(res.data=="图片大小不能超过2M")
        {
            alert("图片大小不能超过2M")
            return;
        }
        data.value.tu = res.data
        imgshow.value = true
    })
}
</script>

<style scoped>

</style>